package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.EquipmentMapper;
import com.kgc.community.pojo.Equipment;
import com.kgc.community.pojo.EquipmentExample;
import com.kgc.community.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    EquipmentMapper equipmentMapper;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public PageInfo<Equipment> searchEquipmentToPage(String condition, Integer pageNum, Integer pageSize, String startTimeStr, String endTimeStr) {

        PageHelper.startPage(pageNum,pageSize);

        if (startTimeStr==null || startTimeStr.length()<=0){
            startTimeStr = "1960-01-01";
        }
        if (endTimeStr==null || endTimeStr.length()<=0){
            endTimeStr = "2060-12-12";
        }

        EquipmentExample example = new EquipmentExample();
        try {
            example.createCriteria().andBuyTimeBetween(format.parse(startTimeStr),format.parse(endTimeStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EquipmentExample.Criteria criteria = example.createCriteria();


        if (condition!=null && condition.length()>0){
            example.createCriteria().andNameLike("%"+condition+"%");

            criteria.andLogoLike("%"+condition+"%");

            example.or(criteria);
        }

        List<Equipment> equipment = equipmentMapper.selectByExample(example);
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipment, 8);

        return pageInfo;
    }

    @Override
    public Integer insertSelective(Equipment equipment) {

        int res = equipmentMapper.insertSelective(equipment);

        return res;
    }

    @Override
    public Integer deleteByIdList(List<Integer> idList) {

        EquipmentExample example = new EquipmentExample();

        idList.add(-1);

        example.createCriteria().andIdIn(idList);

        int res = equipmentMapper.deleteByExample(example);

        return res;
    }

    @Override
    public Integer updateById(Equipment equipment) {

        int res = equipmentMapper.updateByPrimaryKeySelective(equipment);

        return res;
    }

    @Override
    public Equipment searchById(Integer id) {
        Equipment equipment = equipmentMapper.selectByPrimaryKey(id);
        return equipment;
    }
}
